import { Component } from '@angular/core';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';

import { Http, Headers, Response, RequestOptions, RequestMethod, Request } from '@angular/http';
import { app,manage,door } from '../proto/proto';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})


export class AppComponent {
  title = 'app works!';
  private str2arr(str:string) {
    var utf8 = window.unescape(encodeURIComponent(str));
    return new Uint8Array(utf8.split('').map(function (item) {
      return item.charCodeAt(0);
    }));
  }

  constructor(private http: Http){
    //console.log(app);
    let lq = new app.LoginReq();
    lq.Type = 1
    lq.OpenID = '372804854'
    lq.PassWord = '12345678'

    console.log(lq);
    console.log(lq.toJSON());
    console.log(app.LoginReq.encode(lq).finish());
    //console.log(this.http);
    let options1 = new RequestOptions({
        method: RequestMethod.Post,
        headers: new Headers({"Content-Type": "application/x-protobuf", "Authorization": "Basic d2VpbGluX2h0dHA6QFB4dzE3RCM=",
          "User-Platform": "Android"})
    });

    this.http.post("http://120.77.212.19:5555/QueryUsersReq/","",options1)
    .toPromise()
    .then((resp:Response)=>{
        let v3:manage.QueryUsersResp = manage.QueryUsersResp.decode(this.str2arr(resp.text()));
        console.log(v3);
        console.log(v3.UserList);
        let hello = new Uint8Array(resp.text().split('').map((cc)=>{
          return cc.charCodeAt(0);
        }));
        console.log(hello);
        let v4:manage.QueryUsersResp = manage.QueryUsersResp.decode(hello);
        console.log(v4);
    })
    .catch((e:any)=>{
        console.log(e);
        console.log("failedd");
    });

    this.http.post("http://120.77.212.19/LoginReq",app.LoginReq.encode(lq).finish(),options1)
    .toPromise()
    .then((resp:Response)=>{
      console.log('here');
      console.log(resp);
    })
    .catch((e:any)=>{
      console.log('here2');
        console.log("failedd:" +e);
    });
    

  }
}
